﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

namespace WebApplication
{
    public class BaseHandle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "application/json; charset=utf-8";
            if (Request.Form["action"] == null)
            {
                Response.StatusCode = 400;
                Response.Write(JsonConvert.SerializeObject(new { code = 409, msg = "请求方法错误！" }));
                return;
            }

            // 不需要Post请求验证的Action方法
            string[] notAuthorizeAction = new string[] { "Register", "Login" };
            List<string> notAuthorizeActionList = new List<string>(notAuthorizeAction);
            if (!notAuthorizeAction.Contains(Request.Form["action"]))
            {
                if (Session["UID"] == null)
                {
                    Response.StatusCode = 401;
                    Response.Write(JsonConvert.SerializeObject(new { code = 401, msg = "请登录账号！" }));
                    return;
                }
            }

            //反射执行该方法
            var method = GetType().GetMethod(Request.Form["action"]);
            method.Invoke(this, null);
        }
    }
}